<template>
    <view class="container" style="paddingBottom:1rpx;">
      <u-navbar
			leftIconSize="40rpx"
			leftIconColor="#333333"
			title="贷款账单还款明细"
		>
		</u-navbar>
      <view class="section">
        <view class="section-title">{{ title}}</view>
        <view class="form-view">
          <u--form labelPosition="left" :model="form" :rules="rules" ref="uForm" label-width="auto"
            :labelStyle="{ color: '#333333', fontSize: '30rpx' }">
            <u-form-item label="还款日期" prop="repaymentDate" required @click="selectDate()">
              <u--input v-model="form.repaymentDate" disabled disabledColor="#ffffff" placeholder="请选择还款日期" inputAlign="right" border="none"></u--input>
						<u-icon slot="right" name="arrow-right"></u-icon>
            </u-form-item>
            <u-form-item label="还款期数" prop="periods" required >
              <u--input v-model="form.periods"  placeholder="请填写还款期数"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="还款金额" prop="currentAmount"  >
              <u--input v-model="form.currentAmount"  placeholder="请填写还款金额"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="应还本金" prop="principal" required >
              <u--input v-model="form.principal"  placeholder="请填写应还本金"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="利息" prop="interest" required >
              <u--input v-model="form.interest"  placeholder="请填写利息"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="入账状态" prop="postingStateName" required @click="handleShowTeam">
              <u--input v-model="form.postingStateName" disabled disabledColor="#ffffff" placeholder="请选择入账状态"
                inputAlign="right" border="none"></u--input>
              <u-icon slot="right" name="arrow-down"></u-icon>
            </u-form-item>
           
            <u-form-item label="备注" prop="remark"  labelPosition="top">
              <u--textarea v-model="form.remark" placeholder="请填写备注" border="none" autoHeight inputAlign="right" count
                maxlength="20000" style="padding:18rpx 0;"></u--textarea>
            </u-form-item>
          </u--form>
          <view class="form-btn">
            <u-button type="primary" text="提交" @click="submit"></u-button>
          </view>
        </view>
      </view>
      <u-toast ref="uToast"></u-toast>
      <u-picker itemHeight="88" :show="showTeam" :columns="postingStateList" keyName="dictLabel" @cancel="handleCancel"
        @confirm="handleConfirm"></u-picker>
        <u-datetime-picker
			:show="datePickShow"
			mode="date"
      :minDate="-2209017600000"
      ref="createTimeRef" 
			@cancel="datePickShow=false"
			@confirm="datePickConfirm"
			itemHeight="88"
		></u-datetime-picker>
    </view>
  </template>
  
  <script setup>
import {
  getInstallmentDetail,
  addInstallmentDetail,
  updateInstallmentDetail
} from '@/api/invest/installmentDetail'
  import { getDicts } from '@/api/system/dict/data.js'
  const { proxy } = getCurrentInstance()
  import dayjs from 'dayjs'
  import {onLoad,onReady} from "@dcloudio/uni-app";
    // 计算属性与监听属性是在vue中而非uniap中 需要注意！！！
import {reactive ,toRefs,ref,computed ,getCurrentInstance }from "vue";
const datePickShow = ref(false)
const showTeam = ref(false)
const title = ref("贷款账单还款明细")
const postingStateList = ref([])
const data = reactive({
  form: {},
  rules: {
           repaymentDate: [{ type: 'string', required: true, message: '还款日期不能为空', trigger: ['change', 'blur'] }],
           periods: [{ type: 'number', required: true, message: '还款期数不能为空', trigger: ['change', 'blur'] }],
           interest: [{ type: 'number', required: true, message: '利息不能为空', trigger: ['change', 'blur'] }],
           principal: [{ type: 'number', required: true, message: '应还本金不能为空', trigger: ['change', 'blur'] }],
          postingStateName: [{ type: 'string', required: true, message: '入账状态不能为空', trigger: ['change', 'blur'] }],
  }
})
const { form, rules} = toRefs(data)

onLoad((option) => {
       form.value.id = option.id
       form.value.installmentHistoryId = option.installmentHistoryId
       form.value.name = option.name
       if(form.value.id!=null){
        title.value="贷款账单还款明细-修改"
       }else{
        title.value="贷款账单还款明细-新增"
       }
      getDict()
  })
  onReady(() => {
    form.value.createTime = dayjs(new Date().getTime()).format("YYYY-MM-DD HH:mm:ss")
  })
  function  getDict() {
        // 类型
        getDicts('posting_state').then(res => {
        postingStateList.value =[res.data] 
      })
      if(form.value.id!=null){
        getInstallmentDetail(form.value.id).then(res => {
          form.value = res.data
           // 类型
        getDicts('posting_state').then(result => {
          form.value.postingStateName=dictStr(form.value.postingState, result.data)
      })
          
        })
      }
  }
  function dictStr(val, arr) {
      let str = ''
      arr.map(item => {
        if (item.dictValue === val) {
          str = item.dictLabel
        }
      })
      return str
    }

  function handleShowTeam() {
        if (postingStateList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '入账状态为空  ', type: 'warning'
          })
        } else {
          showTeam.value = true
        }
      }
      function   handleConfirm(e) {
        form.value.postingStateName = e.value[0].dictLabel
        form.value.postingState = e.value[0].dictValue
        showTeam.value = false
      }
      function handleCancel() {
       showTeam.value = false
      }
      function  selectDate() {
				datePickShow.value = true
        proxy.$refs['createTimeRef'].innerValue = new Date().getTime()
			}
			function datePickConfirm(e) {
				form.value.createTime = dayjs(e.value).format("YYYY-MM-DD HH:mm:ss")
				datePickShow.value = false
			}
      function submit() {
        proxy.$refs['uForm'].validate().then(() => {
          if (form.value.id != null) {
           
            updateInstallmentDetail(form.value).then(res => {
              proxy.$refs['uToast'].show({
              message: '修改成功', complete() {
                
                uni.navigateTo({ url: `/pages/work/bill/onlineLendHistory/details?id=${form.value.installmentHistoryId}&name=${form.value.bankCardLendName}` })
              }
            })
          })
          }
        })
      }
  </script>
  
  <style lang="scss" scoped>
  .section {
    margin: 24rpx;
    padding: 16rpx 24rpx;
    background-color: #fff;
    border-radius: 8rpx;
  
    .section-title {
      width: 360rpx;
      color: #333333;
      line-height: 44rpx;
      font-size: 30rpx;
      border-left: 6rpx solid #2681FF;
      padding-left: 26rpx;
    }
  
    .form-view {
      padding: 20rpx 0rpx 0 10rpx;
  
      .form-btn {
        padding-top: 20rpx;
      }
    }
  }
  </style>